package com.usth.action;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.bcd.tools.DBUtils;
import com.usth.model.BuyTVO;
import com.usth.model.FlightInfo;
import com.usth.model.FlightVO;
import com.usth.model.SearchVO;
import com.usth.service.ISearchService;

public class SearchAction {
	
private SearchVO search;
private ISearchService searchservice;
private int curpnumber;
private String allsname;
private String pagesname;
private String fid;
private String sdate;
private String landingAirportName;
private String takeOffTime;
private String price;
private BuyTVO bvo;
private String porn;

public String getPorn() {
	return porn;
}
public void setPorn(String porn) {
	this.porn = porn;
}
public BuyTVO getBvo() {
	return bvo;
}
public void setBvo(BuyTVO bvo) {
	this.bvo = bvo;
}
public String getLandingAirportName() {
	return landingAirportName;
}
public void setLandingAirportName(String landingAirportName) {
	try {
		String temp = new String(landingAirportName.getBytes("ISO-8859-1"),"utf-8"); 
		landingAirportName = URLDecoder.decode(temp, "utf-8"); 
		this.landingAirportName = landingAirportName;
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
}
public String getTakeOffTime() {
	return takeOffTime;
}
public void setTakeOffTime(String takeOffTime) {
	this.takeOffTime = takeOffTime;
}
public String getPrice() {
	return price;
}
public void setPrice(String price) {
	this.price = price;
}
public String getSdate() {
	return sdate;
}
public void setSdate(String sdate) {
	this.sdate = sdate;
}
public String getFid() {
	return fid;
}
public void setFid(String fid) {
	this.fid = fid;
}

public int getCurpnumber() {
	return curpnumber;
}

public void setCurpnumber(int curpnumber) {
	this.curpnumber = curpnumber;
}

public String getAllsname() {
	return allsname;
}

public void setAllsname(String allsname) {
	this.allsname = allsname;
}

public String getPagesname() {
	return pagesname;
}

public void setPagesname(String pagesname) {
	this.pagesname = pagesname;
}

public ISearchService getSearchservice() {
	return searchservice;
}

public void setSearchservice(ISearchService searchservice) {
	this.searchservice = searchservice;
}

public SearchVO getSearch() {
	return search;
}

public void setSearch(SearchVO search) {
	this.search = search;
}
public String search() throws UnsupportedEncodingException{
	HttpSession session=ServletActionContext.getRequest().getSession();
//	session.removeAttribute("flights");
//	session.removeAttribute("flightsend");
	session.removeAttribute("flight_s");
	session.removeAttribute("flight_e");
	if(search.getType().equals("S")){
		List<FlightVO> list=this.searchservice.search(this.getSearch());
		session.setAttribute("flights", list);
		session.setAttribute("fspnum", 1);
		DBUtils.page(1, "flights","flight_s");
		this.setSearch(this.getSearch());
	}else 	if(search.getType().equals("D")){
		List<FlightVO> list=this.searchservice.search(this.getSearch());
		session.setAttribute("flights", list);
		DBUtils.page(1, "flights","flight_s");
		this.searchservice.searchD(this.getSearch());
		session.setAttribute("fspnum", 1);
		session.setAttribute("fepnum", 1);
		this.setSearch(this.getSearch());
	}else {
		this.searchservice.searchM(this.getSearch());
		session.setAttribute("fspnum", 1);
		session.setAttribute("fepnum", 1);
		this.setSearch(this.getSearch());
	}
	return "searchtoindex";
}
public String pagecontrol(){
	HttpSession session=ServletActionContext.getRequest().getSession();
String kname="";
if(this.getPagesname().equals("flight_s")){
	kname="fspnum";
}else{
	kname="fepnum";
}
session.setAttribute(kname, this.getCurpnumber()+1);
if(this.getPorn().equals("p")){
	DBUtils.page(this.getCurpnumber()-1, this.getAllsname(), this.getPagesname());
	session.setAttribute(kname, this.getCurpnumber()-1);
}else{
	DBUtils.page(this.getCurpnumber()+1, this.getAllsname(), this.getPagesname());
	session.setAttribute(kname, this.getCurpnumber()+1);
}
return "searchtoindex";
}
public String buy() {
    BuyTVO vo=this.searchservice.buy(sdate, fid, landingAirportName);
    this.setBvo(vo);
    this.getBvo().setPrice(price);
	return "buytickets";
}
}
